package com.appbee.news.client.ui;

import java.util.Set;

import javax.inject.Inject;

import com.appbee.core.client.htmleditor.HtmlEditor;
import com.appbee.core.client.msg.ui.MessageView;
import com.appbee.news.client.activty.IPresenter;
import com.appbee.news.client.text.NewsStrings;
import com.appbee.news.shared.NewsProxy;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Violation;

public class NewsEditor extends Composite
{

	private static NewsEditorUiBinder uiBinder = GWT
			.create(NewsEditorUiBinder.class);

	interface NewsEditorUiBinder extends UiBinder<Widget, NewsEditor>
	{
	}
	
	@UiField(provided=true)
	MessageView msgView;

	@UiField
	Button saveNewsButton;
	
	@UiField 
	Button cancelButton;
	
	@UiField
	TextBox newsTitle;
	
	@UiField(provided = true)
	HtmlEditor newsEditor;
	
	private NewsProxy newsProxy;
	
	private IPresenter presenter;
	
	private final PopupPanel popupPanel = new PopupPanel(true);

	@UiField(provided = true)
	NewsStrings newsStrings;

	@Inject
	public NewsEditor(	MessageView msgView,
						HtmlEditor htmlEditor,
						NewsStrings newsStrings)
	{
		this.msgView = msgView;
		this.newsEditor = htmlEditor;
		this.newsStrings = newsStrings;
		initWidget(uiBinder.createAndBindUi(this));
		popupPanel.setWidget(this);
		saveNewsButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				if(newsProxy==null)
				{
					presenter.onPublishNews(newsTitle.getText(), newsEditor.getHtml());
				}
				else
				{
					newsProxy.setTitle(newsTitle.getText());
					newsProxy.setContent(newsEditor.getHtml());
					presenter.onPublishNews(newsProxy);
				}
			}
		});
		cancelButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				popupPanel.hide();
			}
		});
	}

	public void setPresenter(IPresenter listener)
	{
		this.presenter = listener;
		
	}

	public boolean isShowing()
	{
		return popupPanel.isShowing();
	}

	public void close()
	{
		popupPanel.hide();
	}

	public void showCreateNews(UIObject target)
	{
		msgView.clearMessage();
		newsProxy = null;
		newsTitle.setText("");
		newsEditor.setHtml(SafeHtmlUtils.fromTrustedString(""));
		popupPanel.showRelativeTo(target);
		newsTitle.setFocus(true);
		
	}
	
	public void editNews(NewsProxy newsProxy)
	{
		msgView.clearMessage();
		this.newsProxy = presenter.edit(newsProxy);
		newsTitle.setText(newsProxy.getTitle());
		newsEditor.setHtml(SafeHtmlUtils.fromTrustedString(newsProxy.getContent()));
		newsTitle.setFocus(true);
	}

	public void showCreateNews(int left, int top)
	{
		popupPanel.show();
		popupPanel.setPopupPosition(left, top);
		
	}

	public void showViolations(Set<Violation> errors)
	{
		SafeHtmlBuilder sb = new SafeHtmlBuilder();
		for (Violation violation : errors)
		{
			sb.append(SafeHtmlUtils.fromString(violation.getMessage()));
			sb.appendHtmlConstant("<br>");
		}
		msgView.showError(sb.toSafeHtml());
		
	}
}
